package com.ohyes.bigbang.syscfg.menu.controller;

import java.util.HashMap;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ohyes.bigbang.commons.base.BaseController;
import com.ohyes.bigbang.syscfg.menu.po.Menu;
import com.ohyes.bigbang.syscfg.menu.service.IMenuService;

@Controller
@RequestMapping("/menu")
public class MenuController extends BaseController{

	private static final Logger logger = LoggerFactory
			.getLogger(MenuController.class);

	@Autowired
	IMenuService menuService;

	@RequestMapping("/treeview")
	public String treeView() {
		logger.debug("============treeView==========");
		return "/syscfg/menu/menu_tree";
	}

	@RequestMapping("/getMenuTree")
	public @ResponseBody
	HashMap<String, Object> getMenuTree(int node) {
		HashMap<String, Object> result = new HashMap<String, Object>();
		try {
			List<Menu> menuList = menuService.getMenuTree(node);
			result.put("tree_items", menuList);
			result.put("sucess", true);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}

		return result;
	}

	/**
	 * 保存
	 * 
	 * @param menu
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public HashMap<String, Object> save(Menu menu) {
		HashMap<String, Object> result = new HashMap<String, Object>();
		try {
			if (menu.getId() == 0) {
				menuService.add(menu);
			} else {
				menuService.update(menu);
			}

			result.put("success", true);
		} catch (Exception e) {
			result.put("success", false);
			result.put("msg", e.getMessage());
			logger.error(e.getMessage());
		}

		return result;
	}
	/**
	 * 保存
	 * 
	 * @param menu
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public HashMap<String, Object> delete(int id) {
		HashMap<String, Object> result = new HashMap<String, Object>();
		try {
			menuService.delete(id);

			result.put("success", true);
		} catch (Exception e) {
			result.put("success", false);
			result.put("msg", e.getMessage());
			logger.error(e.getMessage());
		}

		return result;
	}
}
